package com.zzzyh.blogbyspring.utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class MD5SecurityUtils {
    public static String encrypt(String password) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String s = DigestUtils.md5DigestAsHex((uuid + password).getBytes());
        return uuid + s;
    }

    public static boolean verify(String password,String finalPassword){
        if (!StringUtils.hasLength(password) ||
                !StringUtils.hasLength(finalPassword)){
            return false;
        }
        if (finalPassword.length()!=64){
            return false;
        }
        String salt = finalPassword.substring(0,32);
        String securityPassword =
                DigestUtils.md5DigestAsHex((salt + password).getBytes());
        return (salt + securityPassword).equals(finalPassword);
    }
}
